package com.longxi.gamer_base.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;

import com.longxi.gamer_base.R;

/**
 * Created by cxw on & 2018/8/14
 */
public class TextViewUtils {

    public static SpannableString getKeywordContainsShowColor(Context context, String value, String keyword){
        int start = value.indexOf(keyword);
        int end = start + keyword.length();
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(context.getResources().getColor(R.color.tv_color12));
        SpannableString spannableString = new SpannableString(value);
        spannableString.setSpan(colorSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static SpannableString getPayPrice(String price){
        SpannableString spannableString = new SpannableString("支付金额: ￥" + price);
        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(1.5f);
        spannableString.setSpan(sizeSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static SpannableString getPayTotalPrice(String price){
        SpannableString spannableString = new SpannableString("￥" + price);
        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(2f);
        spannableString.setSpan(sizeSpan, 1, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    /**
     * 倒计时前红
     * @return
     */
    public static SpannableString getRedCountDown(Context context, String countDown){
        SpannableString spannableString = new SpannableString(countDown);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(context.getResources().getColor(R.color.tv_color12));
        spannableString.setSpan(colorSpan, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    /**
     * 已成功收款
     * @return
     */
    public static SpannableString getPaymentSuccessPrice(Context context, String price){
        SpannableString spannableString = new SpannableString("已成功收款 " + price);
        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(1.125f);
        spannableString.setSpan(sizeSpan, 5, spannableString.length()-1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    /**
     * 已成功收款
     * @return
     */
    public static SpannableString getPaymentSuccessPrice(Context context, String num, String unit){
        SpannableString spannableString = new SpannableString(num + " " + unit);
        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(1.4f);
        spannableString.setSpan(sizeSpan, 0, num.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(context.getResources().getColor(R.color.color_text7));
        spannableString.setSpan(colorSpan, 0, num.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static SpannableString getPaymentDetailsPrice(String price){
        SpannableString spannableString = new SpannableString(price + " 元");
        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(1.7f);
        spannableString.setSpan(sizeSpan, 1, spannableString.length() - 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static SpannableString getPaymentDetailsPrice2(String price){
        SpannableString spannableString = new SpannableString("合计金额: ￥" + price + "元");
        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(1.5f);
        spannableString.setSpan(sizeSpan, 7, spannableString.length() - 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static SpannableString getStarText(Context context, int starNum){

        String s = "11111";
        final SpannableString spannableString = new SpannableString(s);
        Drawable star = context.getResources().getDrawable(R.mipmap.ic_rate_solid2);
        Drawable star2 = context.getResources().getDrawable(R.mipmap.ic_rate_stroke2);
        CenterAlignImageSpan imageSpan;
        for (int i = 0; i < s.length(); i++) {
            if(i < starNum){
                star.setBounds(0, 0, 45, 45);
                imageSpan = new CenterAlignImageSpan(star);
            }else{
                star2.setBounds(0, 0, 45, 45);
                imageSpan = new CenterAlignImageSpan(star2);
            }
            spannableString.setSpan(imageSpan, i, i+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }

    public static SpannableStringBuilder getStarText(Context context, int starNum, int starNum2, int starNum3){

        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        spannableStringBuilder.append("处理速度: ");
        spannableStringBuilder.append(getStarText(context, starNum));
        spannableStringBuilder.append("\n维修质量: ");
        spannableStringBuilder.append(getStarText(context, starNum2));
        spannableStringBuilder.append("\n服务态度: ");
        spannableStringBuilder.append(getStarText(context, starNum3));
        return spannableStringBuilder;
    }

}
